﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace Business_Layer
{
    [Serializable]
    public class Body : Component
    {
        public Body(int sx, int sy, int ex, int ey, string text, Color color)
            : base(sx, sy, ex, ey, text)
        {
            colorBehaviour = new Colorable();
            colorBehaviour.SetColor(color);

            if (sx > ex)
            {
                int temp = this.sx;
                this.sx = this.ex;
                this.ex = temp;
            }

            if (sy > ey)
            {
                int temp = this.sy;
                this.sy = this.ey;
                this.ey = temp;
            }
        }

        /// <summary>
        /// Change the body's color according to it's colorbehaviour.
        /// </summary>
        /// <param name="color"></param>
        public override void ChangeColor(Color color)
        {
            colorBehaviour.SetColor(color);
        }

        /// <summary>
        /// Draws the body on this specified Graphics component.
        /// </summary>
        /// <param name="g">Graphics</param>
        /// <returns></returns>
        public override Graphics Draw(Graphics g)
        {
            int width = ex - sx;
            int height = ey - sy;
            Rectangle r = new Rectangle(sx, sy, width, height);

            g.FillRectangle(new SolidBrush(colorBehaviour.GetColor()), r);
            g.DrawRectangle(Pens.Black, r);

            if (alternateString != null)
            {
                if (alternateString.StartsWith("mindmap:"))
                {
                    g.DrawImage(Properties.Resources.Mindmap, new Point(ex - 20, ey - 20));
                }
                else if (alternateString.StartsWith("http:"))
                {
                    g.DrawImage(Properties.Resources.Internet, new Point(ex - 20, ey - 20));
                }
                else if (alternateString.StartsWith("file:"))
                {
                    g.DrawImage(Properties.Resources.Folder, new Point(ex - 20, ey - 20));
                }
            }

            using (Font arial = new Font("Arial", 16))
            {
                StringFormat stringFormat = new StringFormat();
                stringFormat.Alignment = StringAlignment.Center;
                stringFormat.LineAlignment = StringAlignment.Center;

                g.DrawString(text, arial, Brushes.Black, r, stringFormat);
            }

            return g;
        }
    }
}